from generated import dingding_pb2
from generated import dingding_pb2_grpc
from core.DingDingRobot import DingDingRobot


class DingDingRobotService(dingding_pb2_grpc.DingDingRobotServiceServicer):
    def __init__(self, config):
        token = config['AccessToken']
        keyword = config['KeyWord']
        self.robot = DingDingRobot(token, keyword)
    def SendMessage(self, request, context):
        """发送普通消息"""
        success = self.robot.send_message(
            msg=request.content,
            at_mobiles=list(request.at_mobiles) if request.at_mobiles else None
        )
        return dingding_pb2.MessageResponse(
            success=success,
            message="消息发送成功" if success else "消息发送失败"
        )

    def SendMarkDown(self, request, context):
        """发送markdown消息"""
        success = self.robot.send_markdown_message(
            title=request.title,
            content=request.content,
            at_mobiles=list(request.at_mobiles) if request.at_mobiles else None
        )

        return dingding_pb2.MarkdownResponse(
            success=success,
            message="消息发送成功" if success else "消息发送失败"
        )

